package com.ziroom.zifamily.zfcensor.service;

import com.ziroom.zifamily.zfcensor.pojo.bo.CensorResultBO;
import com.ziroom.zifamily.zfcensor.mapper.CensorContentEntityMapper;
import com.ziroom.zifamily.zfcensor.mapper.CensorHistoryEntityMapper;
import com.ziroom.zifamily.zfcensor.mapper.entity.CensorHistoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>审核内容操作记录表</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年11月24日 16:09
 * @since 1.0
 */
@Service
public class CensorService {

    @Autowired
    private CensorContentEntityMapper censorContentEntityMapper;

    @Autowired
    private CensorHistoryEntityMapper censorHistoryEntityMapper;

    /**
     * 保存审核结果，同时保存到历史记录中
     * @author cuiyuhui
     * @created
     * @param
     * @return
     */
    public void saveBatchCensorResult(List<CensorResultBO> censorResultBoList) {

        censorResultBoList.stream().forEach( censorResultBo -> {

            Long censorId = censorResultBo.getCensorId();
            String censorCode = censorResultBo.getCensorCode();
            int censorLevel = censorResultBo.getCensorLevel();
            Date censorTime = censorResultBo.getCensorTime();
            int censorStatus = censorResultBo.getCensorStatus();

            int size = censorContentEntityMapper.updateCensorContentByManualCensor(censorResultBo);
            if (size != 1) {
                throw  new RuntimeException("审核内容数据库保存失败");
            }

            //保存审核记录
            CensorHistoryEntity censorHistoryEntity = CensorHistoryEntity.builder()
                    .censorContentId(censorId)
                    .censorCode(censorCode)
                    .censorLevel(censorLevel)
                    .censorStatus(censorStatus)
                    .censorTime(censorTime).build();

            int historySize = censorHistoryEntityMapper.saveCensorHistory(censorHistoryEntity);
            if (historySize != 1) {
                throw  new RuntimeException("审核内容历名数据库保存失败");
            }

        });

    }




}
